﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;


public partial class Default2 : System.Web.UI.Page
{

    public string strConnection;
    OleDbConnection myConn;

    public Default2()
    {
        Page.Init += new System.EventHandler(Page_Init);
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        msg1.Text = "In Page_Load<br/>";

    }


    private void Page_Init(object sender, EventArgs e)
    {
        InitializeComponent();

        string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(".") + "\\test.mdb;";

  
        myConn = new OleDbConnection(strConnection);


    }

    private void InitializeComponent()
    {
        this.btnEnter.Click += new System.EventHandler(this.btnEnter_Click);
        this.Load += new System.EventHandler(this.Page_Load);

    }

    protected void btnEnter_Click(object sender, EventArgs e)
    {
        if(myList.SelectedIndex > 0)
        {
            msg1.Text += "Hi " + name.Text + "<br/>";
            msg1.Text += "   " + password.Text  + "<br/>";
            msg1.Text += myList.SelectedItem;
        }

        string userid, pwd;
        userid = name.Text;
        pwd = password.Text;

       // string mySel="SELECT count(*) as iCount from user where UserID=""+userid+""";
        string mySel="SELECT count(*) as iCount from StuInfo where userid=" +"'"+userid+"'";
    
        OleDbCommand myCmd1=new OleDbCommand(mySel,myConn);

        if (myCmd1.Connection.State == ConnectionState.Open)    //?????????????
            myCmd1.Connection.Close();                          //?????????????

        myCmd1.Connection.Open();
        OleDbDataReader Dr1;
        Dr1=myCmd1.ExecuteReader();
        Dr1.Read();
        string Count=Dr1["iCount"].ToString();
        Dr1.Close();
        myCmd1.Connection.Close();
        string DrPwd,DrRoles;
        if(Count!="0")
        {
            mySel = "SELECT * from StuInfo where userid=" +"'"+userid+"'" ; 
            OleDbCommand myCmd=new OleDbCommand(mySel,myConn);
            myCmd.Connection.Open();
            OleDbDataReader Dr;
            Dr=myCmd.ExecuteReader();
            Dr.Read();
            DrPwd = Dr["pwd"].ToString();
            Dr.Close();
            if(DrPwd==pwd)
            {
                Session["logid"]=userid;//新建一个Session 
                //Response.Redirect("main.aspx");
                msg1.Text += "id: " + userid + "pwd:" + DrPwd;
            }
            else
                msg1.Text="登录密码错.";
        }
        else
            msg1.Text="没有这个用户.";



       
　}
}







    
